package com.proton.carepatchtemp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.NotesAdapter;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Note;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.ReportCenter;
import com.proton.carepatchtemp.utils.ui.ToastUtils;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

public class NotesListActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    private Long reportId;

    private List<Note> mNotes = new ArrayList<>();

    private LinearLayoutManager mLinearLayoutManager;

    private BGANormalRefreshViewHolder mBGANormalRefreshViewHolder;

    private NotesAdapter mNotesAdapter;

    private ImageButton mIbBack;
    private RecyclerView mRcvReportNotesList;
    private BGARefreshLayout mBgaRflNotes;
    private FrameLayout mFrlAddNotes;
    private LinearLayout mLnlNullReport;
    /**
     * 重试
     */
    private TextView mTvNoNetRetry;
    private LinearLayout mLnlReportNoNet;

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.REPORT_GET_NOTES:
                if (event.isSuccess()) {
                    mNotes.clear();
                    mNotes.addAll(event.getList());
                    mNotesAdapter.notifyDataSetChanged();

                    if (mNotes.size() <= 0) {
                        mRcvReportNotesList.setVisibility(View.INVISIBLE);
                        mLnlNullReport.setVisibility(View.VISIBLE);
                    } else {
                        mLnlNullReport.setVisibility(View.GONE);
                    }

                } else {
                    mRcvReportNotesList.setVisibility(View.INVISIBLE);
                    mLnlNullReport.setVisibility(View.VISIBLE);
                    mNotes.clear();
                    mNotesAdapter.notifyDataSetChanged();
                    ToastUtils.e(event.getMsg());
                }
                mBgaRflNotes.endRefreshing();
                mBgaRflNotes.endLoadingMore();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes_list);

        if (!getIntent().hasExtra(Extras.REPORTID) || 0 == getIntent().getLongExtra(Extras.REPORTID, 0)) {
            TastyUtils.makeTextShort(R.string.get_report_failed, TastyToast.ERROR);
            finish();
        } else {
            reportId = getIntent().getLongExtra(Extras.REPORTID, 0);
        }

        initView();
        initRcv();
        setBgaRefreshLayout();
        setListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mBgaRflNotes.beginRefreshing();
    }

    private void initRcv() {
        mLinearLayoutManager = new LinearLayoutManager(NotesListActivity.this, LinearLayoutManager.VERTICAL, false);
        mNotesAdapter = new NotesAdapter(NotesListActivity.this, mNotes);
        mRcvReportNotesList.setLayoutManager(mLinearLayoutManager);
        mRcvReportNotesList.setHasFixedSize(true);
        mRcvReportNotesList.setAdapter(mNotesAdapter);
        mRcvReportNotesList.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    protected void setListener() {
        mIbBack.setOnClickListener(v -> {
            finish();
        });

        mFrlAddNotes.setOnClickListener(v -> {
            Intent addNote = new Intent(NotesListActivity.this, AddNoteActivity.class);
            addNote.putExtra(Extras.REPORTID, reportId);
            startActivity(addNote);
        });

        mTvNoNetRetry.setOnClickListener(v -> {
            mBgaRflNotes.beginRefreshing();
        });
    }


    private void setBgaRefreshLayout() {
        mBgaRflNotes.setDelegate(this);
        mBGANormalRefreshViewHolder = new BGANormalRefreshViewHolder(NotesListActivity.this, true);
        mBGANormalRefreshViewHolder.setLoadingMoreText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setPullDownRefreshText(getString(R.string.string_pull_2_refresh));
        mBGANormalRefreshViewHolder.setRefreshingText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setReleaseRefreshText(getString(R.string.string_release_text));
        mBgaRflNotes.setRefreshViewHolder(mBGANormalRefreshViewHolder);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void initView() {
        mIbBack = findViewById(R.id.ib_back);
        mRcvReportNotesList = findViewById(R.id.rcv_report_notes_list);
        mBgaRflNotes = findViewById(R.id.bga_rfl_notes);
        mFrlAddNotes = findViewById(R.id.frl_add_notes);
        mLnlNullReport = findViewById(R.id.lnl_null_report);
        mTvNoNetRetry = findViewById(R.id.tv_no_net_retry);
        mLnlReportNoNet = findViewById(R.id.lnl_report_no_net);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (NetUtils.noNet()) {
            mBgaRflNotes.endRefreshing();
            mRcvReportNotesList.setVisibility(View.GONE);
            mLnlNullReport.setVisibility(View.GONE);
            mLnlReportNoNet.setVisibility(View.VISIBLE);
            TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
            return;
        } else {
            mLnlReportNoNet.setVisibility(View.GONE);
            mRcvReportNotesList.setVisibility(View.VISIBLE);
        }

        ReportCenter.getCommentList(String.valueOf(reportId));
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
